package com.sz.common.core.system.service.impl;

import com.sz.common.base.dao.Dao;
import com.sz.common.base.utils.CollectionUtils;
import com.sz.common.core.system.service.SysUserSupport;
import com.sz.common.core.system.service.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Function: TODO: ADD FUNCTION <br>
 * Author: Charles. <br>
 * Date: 2017-04-12 10:35:00.
 */
@Service("UserSupport")
public class UserSupportImpl implements SysUserSupport {

    @Autowired
    Dao dao;

    @Override
    public List<UserModel> getUsersById(List<Integer> userIdList) {
        userIdList = CollectionUtils.removeDuplication(userIdList);
        if (userIdList != null && userIdList.size() > 0) {
            List<Map> listMap = dao.findForList("UserMapper.getUsersInfoByIds", userIdList, Map.class);
            return loadUserModel(listMap);
        }
        return null;
    }

    @Override
    public List<UserModel> getUsersByAccount(List<String> accountList) {
        accountList = CollectionUtils.removeDuplication(accountList);
        if (accountList != null && accountList.size() > 0) {
            List<Map> listMap = dao.findForList("UserMapper.getUsersInfoByAccounts", accountList, Map.class);
            return loadUserModel(listMap);
        }
        return null;
    }

    private List<UserModel> loadUserModel(List<Map> listMap) {
        List<UserModel> list = new ArrayList<>();
        if (listMap != null && listMap.size() > 0) {
            for (Map<String, Object> userProps : listMap) {
                int userId = Integer.parseInt(userProps.get("id").toString());
                String accountName = (String) userProps.get("userName");
                String fullName = (String) userProps.get("fullName");
                UserModel model = new UserModel(userId, accountName, fullName);
                list.add(model);
            }
        }
        return list;
    }
}
